package com.inspur.gs.sbssdl.database.bzgl.bzgl.back.bizactions;
import com.inspur.edp.caf.db.dbaccess.DbParameter;
import com.inspur.edp.caf.db.dbaccess.DynamicResultRow;
import com.inspur.edp.caf.db.dbaccess.IDbParameter;
import com.inspur.edp.bef.api.be.*;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.qdp.bql.api.IBqlExecuter;
import com.inspur.edp.qdp.bql.api.OptionType;
import io.iec.edp.caf.runtime.config.BqlBeanUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class sbbzglgetPersonMgrAction extends AbstractManagerAction<String> {
	private String bzmc;
	private String lxbh;
	public sbbzglgetPersonMgrAction(IBEManagerContext managerContext,
			String bzmc, String lxbh) {
		super(managerContext);
		this.bzmc = bzmc;
		this.lxbh = lxbh;
	}

	private final List<IDbParameter> params = new ArrayList<>();

	private final IBqlExecuter bqlExecuter = BqlBeanUtil.getAppCtx().getBean(IBqlExecuter.class);

	@Override
	public  void execute() {
		if (StringUtils.isBlank(bzmc) || StringUtils.isBlank(lxbh)) {
			this.setResult(null);
			return;
		}
		bqlExecuter.getOptions().setOptionType(OptionType.ExecuteSql);
		String sql = " select distinct bzmx.ryid as id from SBSS_BZGL_BZGL bzgl " +
				" join SBSS_BZGL_BZRYMX bzmx on bzgl.ID = bzmx.PARENTID " +
				" where bzgl.BZMC = :bzmc and bzmx.BZRYMC like '%' || :lxmc || '%' ";

		params.add(new DbParameter("bzmc", bzmc));
		params.add(new DbParameter("lxmc", lxbh));

		List<DynamicResultRow> dynamicResultRows = bqlExecuter.executeSelectStatement(sql, new ArrayList<>(), params.toArray(new IDbParameter[]{}));
		StringBuilder ids = new StringBuilder();
		for (DynamicResultRow row : dynamicResultRows) {
			ids.append(row.get("id").toString()).append(",");
		}
		if (ids.length() > 0) {
			ids.delete(ids.length() - 1, ids.length());
		}
		this.setResult(ids.toString());
	}
	private IBusinessEntity getEntity(String dataId) {
		return (IBusinessEntity)super.getBEManagerContext().getEntity(dataId);
	}	private IBEService getMgr() {
		return (IBEService)super.getBEManagerContext().getBEManager();
	}
}